//
//  collectionView.m
//  WLEmotionKeyBoard
//
//  Created by wanglei on 16/12/9.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "collectionView.h"
static const CGFloat toolBarHeight = 36;
static NSString *const ReuseIdentifier = @"ReuseIdentifier";
@implementation collectionView

- (instancetype)init{
    CGFloat w = [UIScreen mainScreen].bounds.size.width / 7;
//    CGFloat magin = (self.superview.bounds.size.height - toolBarHeight - 3 * w) / 4;
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc]init];
    Layout.itemSize = CGSizeMake(w, w);
    Layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    Layout.minimumLineSpacing = 0;
    Layout.minimumInteritemSpacing = 0;
    Layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self = [super initWithFrame:CGRectZero collectionViewLayout:Layout];
    //    注意collectionView的初始化方法必须使用带有collectionViewLayout的那个init方法,否则会报错
        self.delegate = self;
        self.dataSource = self;
      self.backgroundColor = [UIColor whiteColor];
    self.pagingEnabled = true;
    EmoticonManager *emoticonManager = [[EmoticonManager alloc]init];
    NSLog(@"%lu",(unsigned long)emoticonManager.packages[0].emoticonList[1]);
    self.emoticonManager = emoticonManager;
    NSLog(@"self.emoticonManager.packages.count = %lu",(unsigned long)self.emoticonManager.packages[1].emoticonList.count);
     [self registerClass];
    return self;
}

- (void)registerClass{
    [self registerClass:[EmoticonCell class] forCellWithReuseIdentifier:ReuseIdentifier];
}

//实现数据源方法和代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;{
    return self.emoticonManager.packages.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.emoticonManager.packages[section].emoticonList.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    EmoticonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier forIndexPath:indexPath];
    cell.emoticon = self.emoticonManager.packages[indexPath.section].emoticonList[indexPath.item];
//    cell.backgroundColor = indexPath.item % 2 == 0 ? [UIColor redColor] : [UIColor blueColor];
//    NSLog(@"%@",self.emoticonManager.packages[indexPath.section].emoticonList[indexPath.item].code);
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Emoticon *em = _emoticonManager.packages[indexPath.section].emoticonList[indexPath.item];
    
    NSLog(@"em.imagePath = %@,em = %@",em.imagePath,em);
    NSDictionary *dict = [NSDictionary dictionaryWithObject:em forKey:@"key"];
    NSLog(@"dict = %@",dict);
//    注册通知
    NSNotification *notification =[NSNotification notificationWithName:@"dianji" object:nil userInfo:dict];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];

}
-(void)dealloc{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
